//
//  DicViewController.m
//  肿瘤
//
//  Created by 无梦生 on 16/9/14.
//  Copyright © 2016年 Sampson. All rights reserved.
//

#import "DisViewController.h"
#import <AFNetworking.h>
#import "NetWorkTool.h"
#import "DisModel.h"
#import "NSString+category.h"
@interface DisViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@property (nonatomic,weak)UITableView * tableView;

@property(nonatomic,strong)  NSMutableArray *searchArray;

@property (nonatomic,strong)NSArray * disArray;

@property(nonatomic,assign) BOOL isSearchState;

@property(nonatomic,strong) UISearchBar *searchBar;

@end

@implementation DisViewController

-(UISearchBar *)searchBar{

    if (_searchBar == nil) {
        
        _searchBar = [[UISearchBar alloc]init];
        
     
        _searchBar.delegate = self;
    }
    
    return _searchBar;

}

-(NSMutableArray *)searchArray{
    
    if (_searchArray == nil) {
        
        _searchArray = [NSMutableArray array];
        
        
        
        
        
    }
    
    return _searchArray;
}

-(void)setDisArray:(NSArray *)disArray{
    _disArray = disArray;
    [self.tableView reloadData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    DisModel * model = [[DisModel alloc]init];
    NSString *urlString = @"http://iosapi.itcast.cn/searchCI3List.json.php";
    
    NSDictionary *paramDic = @{
                               @"page_size":@15,
                               @"page":@1,
                               @"ci1_id":@1,
                               @"keyword":@""
                               };
    
    [model disWithUrlstring:urlString papmr:paramDic Success:^(NSArray<DisModel *> *arrayy) {
        self.disArray = arrayy;
    } Error:^{
        
    }];

}

#define kCell @"cell"

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"疾病细分";
    [self creatTableView];
    [self creatSearBar];
}

-(void)creatTableView
{
    UITableView * tableView  = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    tableView.dataSource = self;
    
    tableView.delegate = self;
    
    self.tableView = tableView;
    
    [self.view addSubview:tableView];
}

-(void)creatSearBar{

    
    self.navigationItem.titleView =self. searchBar;
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.isSearchState) {
        if (self.searchArray.count > 0) {
            return self.searchArray.count;
        }else{
            return 0;
        }
    }else{
        return self.disArray.count;
    };
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCell];
    DisModel *model;
    if (self.isSearchState) {
        model = self.searchArray[indexPath.row];
    }else{
        model = self.disArray[indexPath.row];
    }
    cell.textLabel.text = model.ci3_name;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DisModel *model;
    if (self.isSearchState) {
        model =self.searchArray[indexPath.row];
    }else{
        model = self.disArray[indexPath.row];
    }
    if (self.block) {
        self.block(model.ci3_name);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)returnText:(void(^)(NSString*str) )block {
    self.block = block;
}


#pragma mark --
#pragma mark --searchBar代理
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if (self.searchArray.count > 0) {
        [self.searchArray removeAllObjects];
    }
    NSString *key = searchText.lowercaseString;
    NSMutableArray *temoArray = [NSMutableArray array];
    if (![key isEqualToString:@""] && ![key isEqual:[NSNull null]] && key != nil) {
        [self.disArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            DisModel *model = self.disArray[idx];
            NSString *name = model.ci3_name;
            NSString *pinYin = model.pinYin;
            NSString *firstLetter = model.nameFirstLetter;
            NSRange rang = [name rangeOfString:key];
            if (rang.length > 0) {
                [temoArray addObject:model];
            }else{
                if ([firstLetter containsString:key]) {
                    [temoArray addObject:model];
                }else{
                    if ([firstLetter containsString:[key substringFromIndex:1]]) {
                        if ([pinYin containsString:key]) {
                            [temoArray addObject:model];
                            
                        }
                    }
                }
            }
        }];
        [temoArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if (![self.searchArray containsObject:temoArray[idx]]) {
                
                [self.searchArray addObject:temoArray[idx]];
            }
        }];
        self.isSearchState = YES;
    }else{
        self.isSearchState = NO;
    }
    
    [self.tableView reloadData];
    
}

@end
















